<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        table {
            margin-top: 20px;
            width: 800px;
            border: 1px solid #ddd;
            border-collapse: collapse;
        }

        td,
        th {
            border: 1px solid #ddd;
            padding: 5px 10px;
        }

        td {
            padding: 0;
            height: 30px;
        }

        td input {
            width: 100%;
            height: 30px;
            border: none;
            background-color: #ffc;
        }
    </style>
</head>
<body>
    <label for="username">姓名</label>
    <input type="text" id="username" name="username">
    <label for="age">年龄</label>
    <input type="text" id="age" name="age">
    <label for="tel">电话</label>
    <input type="text" id="tel" name="tel">
    <label for="qq">QQ号</label>
    <input type="text" id="qq" name="qq">
    <button id="btnSave">保存</button>
    <div class="list">
        <table id="tableList">
            <thead>
                <tr>
                    <th width="40">序号</th>
                    <th width="160">姓名</th>
                    <th width="60">年龄</th>
                    <th width="160">电话</th>
                    <th width="160">QQ号</th>
                    <th width="60">操作</th>
                </tr>
            </thead>
            <tbody></tbody>
        </table>

    <script>
        // 1、获取元素
        var username = document.getElementById('username');
        var age = document.getElementById('age');
        var tel = document.getElementById('tel');
        var qq = document.getElementById('qq');
        var btnSave = document.getElementById('btnSave');
        var tableList = document.getElementById('tableList');
        var tbody = tableList.children[1];
       

       var index=1;
        //2、点击生成表格
        btnSave.onclick=function(){
            var tr=document.createElement('tr')
            
            tr.innerHTML=`
                <td class="name">${index++}</td>
                <td class="name">${username.value}</td>
                <td class="name">${age.value}</td>
                <td class="name">${tel.value}</td>
                <td class="name">${qq.value}</td>
                <td>${'<button class="btnDel">删除</button>'}</td>
            `
            tbody.appendChild(tr);
            // console.log(tr);
            

            //删除行

            //获取所有删除的按钮
            var btnDel=document.getElementsByClassName('btnDel')
            // console.log(btnDel);
            
            for(var i=0;i<btnDel.length;i++){
                btnDel[i].onclick=function(){
                    tbody.removeChild(this.parentNode.parentNode);
                }   
            }
           
            //点击修改表中的内容

            //获取所有class名为name的元素
            var name=document.querySelectorAll('.name');
            console.log(name);
            //遍历name

            var n=false;//true，表示已经有input了
            name.forEach(function(item,index){
                item.onclick=function(){
                   
                    //表示还没有input
                    if(!n){
                        var input=document.createElement('input');
                        input.value=item.innerHTML;
                        item.innerHTML='';
                        item.appendChild(input);
                        input.focurs();
                        n=true;
                    }

                    //当失去焦点时
                    input.onblur=function(){
                        n=false;
                        item.innerHTML=input.value;
                        
                    }

                }
            })



            





        }

        
        




    </script>
</body>
</html>